﻿layui.config({
    version: true,
    base: '/Plugin/'
}).extend({ //设定模块别名
    md5: 'md5', //如果 mymod.js 是在根目录，也可以不用设定别名
    common: 'common', //相对于上述 base 目录的子目录
    jqx:'jqx'//自定义功能
}).use(['element', 'form', 'flow', 'table', 'util', 'layedit', 'laydate', 'common','jqx'], function () {
    var $ = layui.$,
        element = layui.element,
        util = layui.util,
        flow = layui.flow,
        table = layui.table,
        layedit = layui.layedit,
        laydate = layui.laydate,
        common = layui.common,
        jqx = layui.jqx,
        jhy = layui.data("jhy");
    //必须在框架内使用
    common.noneIframe();

    //初始化事件空间
    var firstDate = new Date();
    firstDate.setDate(1); //第一天
    var endDate = new Date(firstDate);
    endDate.setMonth(firstDate.getMonth() + 1);
    endDate.setDate(0);
    laydate.render({
        elem: '#date_start'
        , value: firstDate
        , isInitValue: true
    });
    laydate.render({
        elem: '#date_end'
        , value: endDate
        , isInitValue: true
    });
    //初始化查询
    function loaddiary(date_start, date_end, para) {
        var html = "";
        $.ajax({
            type: "post",
            url: "/Ashx/own.ashx",
            dataType: 'json',
            data: { functionname: "jhy_diary_query", date_start: date_start, date_end: date_end, para: para },
            success: function (msg) {
                if (msg.length > 0) {
                    $.each(msg, function (a, b) {
                        html += "<li class=\"layui-timeline-item\">\
                    <i class=\"layui-icon layui-timeline-axis\">&#xe63f;</i>\
                    <div class=\"layui-timeline-content layui-text\">\
                    <h3 class=\"layui-timeline-title\">"+ util.toDateString(b.diary_datetime, 'yyyy年MM月dd日') + "</h3>\
                    <div class=\"diary_html\">"+ unescape(b.diary_html) + "</div>\
                    <div class=\"layui-btn-container\" style=\"margin-top:10px;\">\
                    <button class=\"layui-btn layui-btn-xs diaryEdit\">修改</button>\
                    <button class=\"layui-btn layui-btn-xs diaryDelete\">删除</button>\
                    </div>\
                    <div style=\"display:none;\">\
                    <span class=\"diary_id\">"+ b.diary_id + "</span>\
                    </div>\
                    </div>\
                    </li>";
                    });
                    $(".timeline").html(html);
                    $(".timeline").find(".diaryDelete").unbind("click");
                    $(".timeline").find(".diaryDelete").bind("click", function () {
                        var spanDelete = $(this);
                        var diary_id = spanDelete.parent().next("div").children(".diary_id").text();
                        layer.confirm('删除确认', { icon: 3, title: '提示' }, function (indexc) {
                            $.ajax({
                                type: "post",
                                url: "/Ashx/own.ashx",
                                data: { functionname: "jhy_diary_delete", diary_id: diary_id },
                                success: function (msg) {
                                    if (msg == "true") {
                                        layer.msg("成功");
                                        spanDelete.parent().parent().parent().remove();
                                    } else {
                                        layer.msg("失败");
                                    }
                                }
                            });
                            layer.close(indexc);
                        });
                    });
                    $(".timeline").find(".diaryEdit").unbind("click");
                    $(".timeline").find(".diaryEdit").bind("click", function () {
                        diaryLayer($(this).parent().parent().parent());
                    });


                } else {
                    $(".timeline").html("无数据");
                }
            }
        });
    }

    //初始显示
    loaddiary("", "", "");
    //查询按钮
    $("#diarySearch").click(function () {
        var date_start = $("#date_start").val();
        var date_end = $("#date_end").val();
        var para = $("#para").val();
        loaddiary(date_start, date_end, para);
    });

    //清除从word中粘贴自带的格式



    //新增按钮操作
    $("#diaryAdd").click(function () {
        var laytext = null;
        parent.layui.layer.open({
            type: 1,
            title: "创建日记",
            content: "<textarea id=\"textareaAdd\" lay-filter=\"textareaAdd\" name=\"desc\" placeholder=\"请输入内容\" class=\"layui-textarea\"></textarea>",
            area: ["100%", "100%"],
            success: function (layero, index) {
                var height = layero.find(".layui-layer-content").height() - 45;
                laytext = parent.layui.layedit.build('textareaAdd', {
                    uploadImage: {
                        url: '/Ashx/uploadImg.ashx',
                        type: 'post'
                    },
                    height: height //设置编辑器高度
                });
            }, btn: ["保存", "关闭"],
            yes: function (index, layero) {
                var diary_html = parent.layui.layedit.getContent(laytext);
                var diary_txt = jqx.clearToText(parent.layui.layedit.getText(laytext));            
                $.ajax({
                    type: "post",
                    url: "/Ashx/own.ashx",
                    data: { functionname: "jhy_diary_add", diary_html: escape(diary_html), diary_txt: diary_txt },
                    success: function (msg) {
                        if (msg == "true") {
                            parent.layui.layer.msg("成功");
                            loaddiary("", "", "");
                            parent.layui.layer.close(index);
                        } else {
                            parent.layui.layer.msg("失败");
                        }
                    }
                });
            }, btn2: function (index, layero) {

            }
        });
    });

    function diaryLayer(li) {
        var laytext = null;
        parent.layui.layer.open({
            type: 1,
            title: "修改日记",
            content: "<textarea id=\"textareaEdit\" lay-filter=\"textareaEdit\" name=\"desc\" placeholder=\"请输入内容\" class=\"layui-textarea\">" + li.find(".diary_html").html() + "</textarea>",
            area: ["100%", "100%"],
            success: function (layero, index) {
                var height = layero.find(".layui-layer-content").height() - 45;
                laytext = parent.layui.layedit.build('textareaEdit', {
                    uploadImage: {
                        url: '/Ashx/uploadImg.ashx',
                        type: 'post'
                    },
                    height: height //设置编辑器高度
                });
            }, btn: ["保存", "关闭"],
            yes: function (index, layero) {
                var diary_html = parent.layui.layedit.getContent(laytext);
                var diary_txt = parent.layui.layedit.getText(laytext);
                var diary_id = li.find(".diary_id").text();
                $.ajax({
                    type: "post",
                    url: "/Ashx/own.ashx",
                    data: { functionname: "jhy_diary_edit", diary_id: diary_id, diary_html: escape(diary_html), diary_txt: diary_txt },
                    success: function (msg) {
                        if (msg == "true") {
                            parent.layui.layer.msg("成功");
                            li.find(".diary_html").html(diary_html);
                            parent.layui.layer.close(index);
                        } else {
                            parent.layui.layer.msg("失败");
                        }
                    }
                });
            }, btn2: function (index, layero) {

            }
        });
    }
});


